<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('jquery-validate/lib/jquery.metadata.js'))
->appendFile($this->js('jquery-validate/jquery.validate.min.js'))
->appendFile($this->js('plugins/jquery.selectboxes.js'))
->captureStart()?>
$.metadata.setType("attr", "validate");
// 課程節數

$(document).ready(function(){
	getTeacherCourse();
	getAllCount();
	$("#sel_teacher").change(getTeacherCourse);

	$("#year_semester").change(function(){
		window.location.href = '<?php echo  $this->route('index')?>/index/year_semester/'+$(this).val();
	});

	$("#myform").ajaxForm({
		success : function(){
			getTeacherCourse();
			getClassCourse();
			getAllCount();
		},
		beforeSubmit : ajaxValidate
	});

	function ajaxValidate() {

		return $("#myform").validate({
			rules : {
				number: {required: true , number: true,  min: 1, max: 10}
			},
			messages:{
				sel_grade : "<?php echo $this->translate('Must choose a class')?>",
				sel_course : "<?php echo $this->translate('Must choose a subject')?>",
				number: {
					required: "<?php echo $this->translate('The number of hours can not be left blank')?>",
					number : "<?php echo $this->translate('Must be a number')?>",
					min : "<?php echo $this->translate('Must be greater than 0')?>",
					max : "<?php echo $this->translate('Must be greater than 0')?>"
				}
			}
		}).form();

	}


	// 取科目列表
	$("#sel_grade").change(function(){
		$("#sel_grade_key").val($(this).val());
		var class_id = $(this).val();

		var year_semester = $("#year_semester").val();
		$.getJSON('<?php echo  $this->route('courselist')?>/year_semester/'+year_semester+'/class_id/'+class_id,
		function(data){
			$("#sel_course").removeOption(/./);
			$("#sel_course").addOption(data,false);

			getClassCourse();
		});

	});



});

// 教師配課列表
function getTeacherCourse() {

		$.get('<?php echo  $this->route('teachercourse')?>/year_semester/'+$("#year_semester").val()+'/teacher_sn/'+$("#sel_teacher").val(),
		function(data){
			$("#course_list").empty();
			$("#course_list").append('<h2>'+$("#sel_teacher option:selected").text()+' <?php echo $this->translate('Configuration course situation')?></h2>');
			$("#course_list").append(data);
			sumCourse();
			 getAllCount();
             getClassCourse();
		});
	}

// 班級配課情形
	function getClassCourse() {
		if ($("#sel_grade").val()=='')
			return true;
		$.get('<?php echo  $this->route('classcount')?>/year_semester/'+$("#year_semester").val()+'/class_id/'+$("#sel_grade").val(),function(data){
			$("#class_course_list").empty();
			$("#class_course_list").append('<h2>'+$("#sel_grade option:selected").text()+' <?php echo $this->translate('Configuration course situation')?></h2>');
			$("#class_course_list").append(data);
			var sum = 0;
			$.each($(".c2-num"),function(i,ss){
				sum += parseInt($(ss).html());
			})
			$("#class_course_list").append('<p id="sum"><?php echo $this->translate('total')?>:'+sum+'<?php echo $this->translate('sections')?></p>');
			});
	}

// 所有教師配課列表
function getAllCount() {
	$.get('<?php echo  $this->route('teachercount')?>/year_semester/'+$("#year_semester").val(),
	function(data){
		$("#course_count").html(data);
		//sumCourse();

	});
}


// 統計時數
function sumCourse() {
		$("#sum").remove();
		var sum = 0;
		$.each($(".c-num"),function(i,ss){
			sum += parseInt($(ss).html());
		})
		$("#course_list").append('<p id="sum"><?php echo $this->translate('total')?>:'+sum+'<?php echo $this->translate('sections')?></p>');
		var sum = 0;


}

// 刪除配課
$(".delcourse").live('click',function(){
	var id = $(this).attr('id').substr(2);
	$.get('<?php echo  $this->route('delete')?>/id/'+id,function(data){
	   if (data == '0')
	       alert('<?php echo $this->translate('There Course Scheduling information can not be deleted')?>');
	     else {
    		$("#d-"+id).parents("tr").remove();
    		sumCourse();
    		getAllCount();
    		getClassCourse();
    	}
	});

});

// 選取一位教師,重設配課
$(".sel-teacher").live('click',function(){
	var id =$(this).attr('id').substr(2);
	$("#sel_teacher").val(id);
	$("#sel_teacher").trigger('change');

});

// 點選班級 event trigger
$(".sel-class").live('click',function(){
        var id =$(this).attr('id').substr(2);
        $("#sel_grade").val(id);
        $("#sel_grade").trigger('change');
 });


<?php $this->headScript()->captureEnd()?>
<?php $this->headStyle()->captureStart()?>
label.error {
	margin-left: 10px;
	width: auto;
	display: inline;
	color:red;
}

#course_count{
margin:10px;
height:300px;
width:215px;
border:1px #aaa solid;
overflow: auto
}

#course_count table{
width:200px;
}

#set_information {
float:left;
padding:10px;
}

#set_information table{
	width:300px;
	border:1px #aaa solid;
	background:#dde;
}

#course_list  tbody  tr {
	text-align:center;
	background:#ffe;
	padding:3px;
}

#class_course_list tbody tr {
	text-align:center;
	background:#fde;
	padding:3px;
}

#course_count td {
	text-align:center;
	background:#eff;
	padding:3px;
}

#course_list .c-num {text-align:right}

#setform {float:left;margin:10px;border:1px solid #ccc;padding:5px;width:300px}

.sel_teacher {background:#ff0;text-weight:bold}


<?php $this->headStyle()->captureEnd()?>

<form id="myform" method="post" class="cmxform" action="<?php echo  $this->route('set')?>" />
<div id="setform">
<p>

<select name="year_semester" class="refresh" id="year_semester">
<?php echo  $this->YearSemesterOptions($this->year_semester) ?>
</select>
<select name="sel_teacher" id="sel_teacher">
<?php echo  $this->TeacherNameOptions($this->year_semester)?>
</select>
</p>
<p>
<label for="sel_grade" ><?php echo $this->translate('Teach class')?> : </label>
<select name="sel_grade" id="sel_grade" validate="required:true" >
<option value=""><?php echo $this->translate('Select class')?></option>
<?php echo  $this->classNameOptions($this->year_semester)?>
</select>
</p>
<p>
<label for="sel_course" ><?php echo $this->translate('Teach subject')?> : </label>
<select name="sel_course" id="sel_course" validate="required:true">
<option value=""><?php echo $this->translate('Select subject')?></teacheroption>
</select>
</p>
<p>
<label for="number"><?php echo $this->translate('The number of teaching')?> : </label>
<input type="text" name="number" id="number" class="required"  size="5" tabindex="3" />
</p>

<p>
<input type="submit" name="submit" id="submit" value="<?php echo $this->translate('confirm')?>" />
</p>
</div>

<div id="set_information">
	<div id="course_list"> <!-- 教師配課表 -->
	</div>
	<div id="class_course_list"> <!-- 班級配課表 -->

	</div>
</div>
<div id="course_count">
</div>
</form>
<div style="clear:both;"></div>